<script setup lang="ts">
import { useLoginService } from '@/api/user'
import { useUserStore } from '@/stores'
import type { IAccount } from '@/types'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const formModel = ref<IAccount>({
  username: '',
  password: ''
})

const rules: object = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ]
}

const router = useRouter()
const userStore = useUserStore()
const form = ref()

const login = async () => {
  await form.value.validate()
  const { data } = await useLoginService(formModel.value)
  userStore.setToken(data.token)
  console.log(data.token, 'kankan')
  ElMessage.success('登录成功')
  router.push('/')
}
</script>

<template>
  <div class="login-page">
    <el-row class="login-main">
      <el-card>
        <div   class="form">
          <el-form
            ref="form"
            :model="formModel"
            :rules="rules"
            size="large"
            autocomplete="off"
          >
            <el-form-item>
              <h1 class="login-class">登录</h1>
            </el-form-item>
            <el-form-item   prop="username">
              <el-input
                :prefix-icon="User"
                v-model="formModel.username"
                placeholder="请输入用户名"
              ></el-input>
            </el-form-item>
            <el-form-item   prop="password">
              <el-input
                name="password"
                :prefix-icon="Lock"
                type="password"
                v-model="formModel.password"
                placeholder="请输入密码"
              ></el-input>
            </el-form-item>
            <el-form-item class="flex">
              <div class="flex">
                <!-- <el-checkbox>记住我</el-checkbox> -->
              </div>
            </el-form-item>
            <el-form-item>
              <el-button
                class="button"
                type="primary"
                @click="login"
                auto-insert-space
                >登录</el-button
              >
            </el-form-item>
          </el-form>
        <!-- </el-col> -->
        </div>
      </el-card>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.login-page{
  background-color: #3c99c2;
  .login-main {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
      background: 
        url('@/assets/logo.png') no-repeat center 6% / 220px auto,
        url('@/assets/login_bg.jpg') no-repeat center / cover;
    .login-class {
      padding-bottom: 5%;
      margin: 0 auto;
    }
  
    .form {
      display: flex;
      flex-direction: column;
      justify-content: center;
      user-select: none;
      padding: 10px 15px;
      .title {
        margin: 0 auto;
        font-weight: 700;
      }
      .button {
        width: 100%;
        margin-right: -20px;
      }
      .flex {
        width: 100%;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>
